package com.sdp.wx.controller;


import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.TreeSet;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sdp.core.bizc.db.Query;
import com.sdp.core.bizc.vo.DataCenter;
import com.sdp.core.bizc.vo.Result;
import com.sdp.wx.db.wx_repair_must_item.entity.WxRepairMustItem;
import com.sdp.wx.db.wx_repair_must_item.service.IWxRepairMustItemService;
import com.sdp.wx.db.wx_repair_must_item.service.impl.WxRepairMustItemServiceImpl;
import com.sdp.wx.view.WxRepairMustItemView;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zsp
 * @since 2024-02-19
 */
@RestController
@RequestMapping("/WxRepairMustItemController")
public class WxRepairMustItemController {
	
	@Autowired
	private Query query;
	
	@Autowired
	private WxRepairMustItemServiceImpl wxRepairMustItemServiceImpl;
	
	@Autowired
	private IWxRepairMustItemService iWxRepairMustItemService;
	
	@PostMapping(value = "/viewPage")
	public Result<?> viewPage(@RequestBody DataCenter dataCenter) {
		QueryWrapper<WxRepairMustItemView> queryWrapper = new QueryWrapper<>();
		return Result.ok(query.viewPage(wxRepairMustItemServiceImpl, WxRepairMustItemView.class, dataCenter, queryWrapper));
	}
	
	@PostMapping(value = "/viewList")
	public Result<?> viewList(@RequestBody DataCenter dataCenter) {
		QueryWrapper<WxRepairMustItemView> queryWrapper = new QueryWrapper<>();
		return Result.ok(query.viewList(wxRepairMustItemServiceImpl, WxRepairMustItemView.class, dataCenter, queryWrapper));
	}
	
	@PostMapping(value = "/save")
	@Transactional(rollbackFor=Exception.class)
	public synchronized Result<?> list(@RequestBody JSONObject obj) {
		if(obj==null){
			return Result.error("项目信息不存在");
		}
		String repairType = obj.getString("repairType");
		if(StringUtils.isEmpty(repairType)) {
			return Result.error("维修类型为空");
		}
		List<WxRepairMustItem> items = JSONArray.parseArray(JSON.toJSONString(obj.get("itemList")), WxRepairMustItem.class);
		for(WxRepairMustItem item : items) {
			item.setKeyId(null);
			item.setRepairType(repairType);
		}
		
		if(!StringUtils.isEmpty(repairType) && items!=null && items.size()>0) {
			List<WxRepairMustItem> itemList = iWxRepairMustItemService.queryRepairMustItemByRepairType (repairType);
			if(itemList==null || itemList.size()==0) {
				iWxRepairMustItemService.saveBatch(items);
			}else {
				itemList.addAll(items);
				iWxRepairMustItemService.saveOrUpdateBatch(itemList.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(()->new TreeSet<>(Comparator.comparing(WxRepairMustItem :: getItemNo))), ArrayList::new)));
			}
		}
		return Result.ok();
	}

	@GetMapping(value = "/del")
	@Transactional(rollbackFor=Exception.class)
	public Result<?> del(@RequestParam(name="keyId",required=true) String keyId) {
		if(StringUtils.isEmpty(keyId)) {
			return Result.error("需要删除的记录信息不完整");
		}
		iWxRepairMustItemService.removeById(keyId);
		return Result.ok();
	}
}

